package com.dpj;

import com.dpj.mapper.BrandMapper;
import com.dpj.mapper.UserMapper;
import com.dpj.pojo.Brand;
import com.dpj.pojo.User;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMyBatis {
    public static void main(String[] args) throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
//        User user = (User) sqlSession.selectOne("com.dpj.mapper.UserMapper.selectById", 1);
//        System.out.println(user);

//        List<User> users = sqlSession.selectList("com.dpj.mapper.UserMapper.selectAll");
//        System.out.println(users);

        //3.采用代理的模式 进行sql的调用
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> allUser = mapper.getAllUser();
        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        List<Brand> allBrand = mapper1.getAllBrand();

        System.out.println(allUser);
        System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }
}
